package com.minyifang.wcspf.work.ui.goods.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.minyifang.wcspf.R;
import com.minyifang.wcspf.common.utils.ImageUtils;
import com.minyifang.wcspf.work.ui.goods.model.GoodsBanner;

import java.util.List;

import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;

public class BannerPagerAdapter extends PagerAdapter {
        private List<GoodsBanner> mBannerList;
        private Context mContext;

        public BannerPagerAdapter(List<GoodsBanner> bannerList, Context context) {
            mBannerList = bannerList;
            mContext = context;
        }

        @Override
        public int getCount() {
            return mBannerList.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            View layout = LayoutInflater.from(mContext).inflate(R.layout.item_goods_banner, null);
            ImageView imageView = layout.findViewById(R.id.iv_picture);
            JZVideoPlayer videoPlayer = layout.findViewById(R.id.videoplayer);
            container.addView(layout);
            GoodsBanner banner = mBannerList.get(position);
            if (banner.isVideo()) {
                videoPlayer.setVisibility(View.VISIBLE);
                videoPlayer.setUp(banner.url, JZVideoPlayerStandard.SCREEN_WINDOW_NORMAL, "");
            } else {
                videoPlayer.setVisibility(View.GONE);
                ImageUtils.getInstance().showImage( mBannerList.get(position).url, imageView);
            }
            return layout;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
        }
    }